<?php
namespace App\Model;

use PhalApi\Model\NotORMModel as NotORM;

class WALLETADDRESS extends NotORM
{

    protected function getTableName($id)
    {
        return 'wallet_address';
    }

    public function test()
    {
        $time = time();
        $sql = "select id from byb_user where token is null";
        $data = $this->getORM()->queryAll($sql);
        foreach($data as $v)
        {
            $sql = "update byb_user set token = '". md5($v['id']) ."'  where id = ".$v['id'];
            $this->getORM()->queryAll($sql);
        }
        var_dump(time() - $time);die;
        $sql = 'update user set token =  where token is null';

    }

    public function getWalletByToken($token) {
        $sql = 'SELECT w.wallet_userid,w.id,w.wallet_address,u.id as uid,w.paymentpwd,w.qrcode,u.telphone,u.wallet '
            . 'FROM byb_wallet_address AS w LEFT JOIN byb_user AS u '
            . 'ON u.id = w.uid '
            . 'WHERE u.token = ?';
        $data = $this->getORM()->queryAll($sql, [$token]);
        if(!empty($data))
            return $data[0];
    }

    public function getAllWallet()
    {
        $data = $this->getORM()->where('wallet_address != ?', '')->fetchAll();
        return $data;
    }

    public function getWalletByAddress($address)
    {
        $data = $this->getORM()->where('wallet_address = ?', $address)->fetchOne();
        return $data;
    }

}